<template>
  <div
    class="product-two-col-wrap j_akc_ubt"
    :class="{ vertical: isVerticalNav }"
    @click="goProductDetail"
  >
    <div class="img-wrap-box" :class="{ vertical: isVerticalNav }">
      <img v-lazy="imgUrl" :class="{ vertical: isVerticalNav }" />
      <!-- [省赚] 组件 -->
      <profit-tag
        v-if="isShowProfit"
        class="profit--private"
        :class="{ vertical: isVerticalNav }"
        :profitObj="product"
        :isVerticalNav="isVerticalNav"
      />
      <div v-if="product.sellOut" class="sellout">
        售罄
      </div>
      <!-- 打标 -->
      <img
        v-if="product.productSubscriptUrl"
        class="mark-img"
        :src="product.productSubscriptUrl"
      />
    </div>
    <div class="footer-wrap">
      <p class="product-des more_text">
        {{ product.productName || '' }}
      </p>
      <div class="price">
        <span class="unit">¥</span><span>{{ product.price }}</span
        ><span class="linePrice">¥{{ product.tagPrice }}</span>
      </div>
      <div v-if="product.sellOut" class="buy-btn buy-over">
        已抢光
      </div>
      <div v-else class="buy-btn" @click.stop="handleBtnClick">
        <i class="icon-buy" :class="{ 'icon-share': isUserOfB }"></i
        ><span>{{ isUserOfB ? '转发' : '去抢购' }}</span>
      </div>
    </div>
  </div>
</template>

<script>
import { formatImgUrl, http2https } from '@/utils/util';
import ProfitTag from '@/components/profit.vue'; // [省赚] 组件

export default {
  name: 'productTwoCol',
  components: {
    ProfitTag
  },
  props: {
    product: {
      type: Object,
      default: () => {}
    },
    index: {
      type: Number,
      default: 0
    },
    isVerticalNav: {
      type: Boolean,
      default: false
    }
  },
  computed: {
    imgUrl() {
      let { productPicUrl } = this.product || {};
      if (productPicUrl) {
        productPicUrl = formatImgUrl(productPicUrl.split(',')[0], 174, 174);
        return http2https(productPicUrl);
      }
      return null;
    },
    isUserOfB() {
      return true;
    },
    isShowProfit() {
      const { profit } = this.product || {};
      return this.isUserOfB && profit;
    }
  },
  methods: {
    // 前往商品详情页
    goProductDetail() {
      this.$emit('goProductDetail');
    },
    handleBtnClick() {
      if (this.isUserOfB) {
        this.$emit('shareProduct');
      } else {
        this.$emit('addToCart');
      }
    }
  }
};
</script>

<style lang="scss" scoped>
.product-two-col-wrap {
  width: 174px;
  background-color: #fff;
  border-radius: 10px;
  overflow: hidden;
  margin-bottom: 10px;

  &.vertical {
    width: 150px;
  }

  .img-wrap-box {
    position: relative;
    width: 174px;
    height: 174px;
    &.vertical {
      width: 150px;
      height: 150px;
    }
    img {
      width: 174px;
      height: 174px;
      object-fit: cover;
      border-radius: 10px 10px 0 0;
      display: inline-block;
      &.vertical {
        width: 150px;
        height: 150px;
      }
    }

    .profit--private {
      max-width: 174px;

      &.vertical {
        max-width: 150px;
      }
    }
    .mark-img {
      position: absolute;
      top: 0;
      right: 10px;
      width: 26px;
      height: 29px;
      object-fit: cover;
      display: block;
      border-radius: 0;
    }
  }
  .sellout {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    margin: auto;
    display: flex;
    justify-content: center;
    align-items: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    color: #fff;
    font-size: 12px;
    background-color: rgba(0, 0, 0, 0.6);
  }
  .footer-wrap {
    width: 100%;
    padding: 8px;
    box-sizing: border-box;
  }
  .product-des {
    width: 100%;
    font-size: 12px;
    font-family: PingFangSC-Regular;
    color: #333;
  }
  .price {
    font-size: 20px;
    font-family: DINAlternate-Bold, DINAlternate;
    font-weight: bold;
    color: #fe1e3d;
    line-height: 20px;
    margin-top: 8px;
  }
  .unit {
    font-size: 12px;
    margin-right: 2px;
    color: #fe1e3d;
    font-family: DINAlternate-Bold, DINAlternate;
    font-weight: bold;
  }

  .linePrice {
    text-decoration: line-through;
    font-size: 12px;
    font-family: DINAlternate-Bold, DINAlternate;
    font-weight: bold;
    color: #999;
    line-height: 14px;
    margin-left: 4px;
  }
  .buy-btn {
    width: 100%;
    height: 28px;
    background: #ff1010;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    font-size: 14px;
    font-weight: 600;
    border-radius: 14px;
    margin-top: 8px;
    line-height: normal;
  }
  .icon-buy {
    width: 8px;
    height: 14px;
    background: url('../../../assets/superBrand/icon-buy.png') no-repeat;
    background-size: 100% 100%;
  }
  .icon-share {
    width: 20px;
    height: 20px;
    margin-right: 5px;
    margin-top: -1px;
    background: url('../../../assets/zhuanfa_white_icon.png') no-repeat;
    background-size: 100% 100%;
  }
  .buy-over {
    opacity: 0.4;
  }
  .more_text {
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    word-break: break-all;
    height: 34px;
    line-height: 17px;
    overflow-y: hidden;
  }
}
</style>
